package com.zhaowb.springcloud.admin.biz;

import com.zhaowb.springcloud.admin.entity.User;
import com.zhaowb.springcloud.admin.mapper.UserMapper;
import com.zhaowb.springcloud.common.biz.BaseBiz;
import com.zhaowb.springcloud.common.constant.UserConstant;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * Created with IDEA
 *
 * @author zhaowb
 * @date 2018/11/1 21:29
 */
@Service
public class UserBiz extends BaseBiz<UserMapper, User> {

    @Override
    public void insertSelective(User user) {
        String password = new BCryptPasswordEncoder(UserConstant.PW_ENCODER_SALT).encode(user.getPassword());
        user.setPassword(password);
        super.insertSelective(user);
    }

    @Override
    public void updateSelectiveById(User user) {
        String password = new BCryptPasswordEncoder(UserConstant.PW_ENCODER_SALT).encode(user.getPassword());
        user.setPassword(password);
        super.updateById(user);
    }

    /**
     * 根据用户名获取用户信息
     *
     * @param username 用户名
     * @return
     */
    public User getUserByUsername(String username) {
        User user = new User();
        user.setUsername(username);
        return super.selectOne(user);
    }

}
